<?php
include_once('config/config.php');
$auth = check_auth();
include_once('./config/logout.php');
// 当前页
$page = $_GET['page'] ?? 1;

// 每页显示多少条数据
$limit = 3;

// 页码
$size = 5;

// 获取偏移量
$start = ($page - 1) * $limit;

// 获取数据
$depSql = "SELECT `id`,`name` FROM `pre_dep` ORDER BY `id` DESC LIMIT $start,$limit";

// 获取数据总数
$countSql = "SELECT COUNT(*) FROM `pre_dep`";

$count = find($countSql);

// 查询
$depData = finds($depSql);

$showPage = showPage($page, $count['COUNT(*)'], $limit);
?>
<!DOCTYPE html>
<html lang="en">

<head>
	<?php include_once('common/meta.php'); ?>
</head>

<body>
	<?php include_once('common/header.php'); ?>
	<?php include_once('common/menu.php'); ?>
	<div class="content">
		<div class="header">
			<h1 class="page-title">部门列表</h1>
		</div>
		<ul class="breadcrumb">
			<li><a href="index.html">Home</a> <span class="divider">/</span></li>
			<li class="active">Index</li>
		</ul>

		<div class="container-fluid">
			<div class="row-fluid">
				<div class="btn-toolbar">
					<button class="btn btn-primary" onClick="location='depAdd.php'"><i class="icon-plus"></i>添加部门</button>
				</div>
				<div class="well">
					<table class="table">
						<thead>
							<tr>
								<th>ID</th>
								<th>部门名称</th>
								<th style="width: 26px;"></th>
							</tr>
						</thead>
						<tbody>
							<?php foreach ($depData as $item) { ?>
								<tr>
									<td><?php echo $item['id']; ?></td>
									<td><?php echo $item['name']; ?></td>
									<td>
										<a href="depEdit.php?id=<?php echo $item['id']; ?>"><i class="icon-pencil"></i></a>
										<a href="#myModal" data-id="<?php echo $item['id']; ?>" role="button" data-toggle="modal"><i class="icon-remove"></i></a>
									</td>
								</tr>
							<?php } ?>
						</tbody>
					</table>
				</div>
				<div class="pagination">
					<ul>
						<?php echo $showPage; ?>
					</ul>
				</div>

				<div class="modal small hide fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
					<div class="modal-header">
						<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
						<h3 id="myModalLabel">删除部门</h3>
					</div>
					<div class="modal-body">
						<p class="error-text"><i class="icon-warning-sign modal-icon"></i>是否确认删除部门？</p>
					</div>
					<div class="modal-footer">
						<button class="btn" data-dismiss="modal" aria-hidden="true">取消</button>
						<button class="btn btn-danger delete" data-dismiss="modal">删除</button>
					</div>
				</div>

				<?php include_once('common/footer.php') ?>

			</div>
		</div>
	</div>
</body>

</html>
<?php include_once('common/script.php') ?>
<script>
	// 全局部门id
	let id = null;

	$('a[href="#myModal"]').click(function() {
		// 获取类似data-dismiss以data为前缀的值
		id = $(this).data('id'); // js dateset
	});

	$('.delete').click(function() {
		$.ajax({
			type: 'post',
			url: 'api.php',
			data: {
				id,
				action: 'dep_del'
			},
			dataType: 'json',
			success: function(res) {
				if (res && res.code === 1) {
					alert(res.msg);
					location.reload();
					return;
				} else {
					alert(res.msg);
					return;
				}
			}
		})
	});
</script>